import 'package:flutter/material.dart';
import 'package:flutterDemo/common_ui/common_style.dart';
import 'package:flutterDemo/pages/auth/auth_vm.dart';
import 'package:flutterDemo/pages/auth/register_page.dart';
import 'package:flutterDemo/pages/tab_page.dart';
import 'package:flutterDemo/route/RouteUtils.dart';
import 'package:provider/provider.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  AuthViewModel viewModel = AuthViewModel();
  TextEditingController? userController;
  TextEditingController? pwdController;

  @override
  void initState() {
    super.initState();
    userController = TextEditingController();
    pwdController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (context) {
          return viewModel;
        },
        child: Scaffold(
            backgroundColor: Colors.teal,
            body: Container(
              padding: const EdgeInsets.only(left: 20, right: 20),
              alignment: Alignment.center,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  commonInput(labelText: "请输入账号", controller: userController),
                  const SizedBox(
                    height: 20,
                  ),
                  commonInput(
                    labelText: "请输入密码",
                    obscureText: true,
                    controller: pwdController,
                  ),
                  const SizedBox(
                    height: 50,
                  ),
                  //按钮
                  whiteBorderButton(
                      title: "开始登录",
                      onTap: () {
                        viewModel.setLoginInfo(
                            username: userController?.text ?? "",
                            password: pwdController?.text ?? "");
                        viewModel.login().then((value) {
                          if (value == true) {
                            print("登录成功");
                            RouteUtils.pushAndRemoveUntil(
                                context, const TabPage());
                          }
                        });
                      }),
                  const SizedBox(height: 5),
                  //注册
                  registerButton(() {
                    RouteUtils.push(context, RegisterPage());
                  })
                ],
              ),
            )));
  }

  Widget registerButton(GestureTapCallback? onTap) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
          alignment: Alignment.center,
          width: 100,
          height: 45,
          child: const Text(
            "注册",
            style: TextStyle(color: Colors.white, fontSize: 13),
          ),
        ));
  }
}
